function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

$(document).ready(function(){
    $.get('/api/v1.0/areas',function (resp) {
       if (resp.errno == '0'){
           // for (let i of resp.data){
           //     $('#area-id').append(`<option value="${i.id}">${i.name}</option>`)
           // }

           // 使用template模板语法 基于Jquery的
           let html = template("areas_template",{areas:resp.data});
           $('#area-id').html(html);
       }else {
           alert(resp.errmsg)
       }
    },"json")

    $('#form-house-info').submit(function (e) {
        e.preventDefault();

        let data = {};
        $('#form-house-info').serializeArray().map(function (x) {
            data[x.name] = x.value
        });

        let facility = [];
        $(':checked[name=facility]').each(function (index,x) {
            facility[index] = $(x).val()
        });
        data.facility = facility;
        $.ajax({
            url:'/api/v1.0/houses/info',
            type:'post',
            'contentType':"application/json",
            data:JSON.stringify(data),
            dataType:'json',
            headers:{
                "X-CSRFTOKEN":getCookie("csrf_token")
            },
            success:function (resp) {
                if (resp.errno == '4101'){
                    location.href = '/login.html'
                }else if(resp.errno == '0'){
                    $('#form-house-info').hide();
                    $('#form-house-image').show();
                    // 设置图片表单中的house_id
                    $('#house-id').val(resp.data.house_id)
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    });

    $('#form-house-image').submit(function (e) {
        e.preventDefault();
        $(this).ajaxSubmit({
            url:'/api/v1.0/houses/image',
            type:'post',
            dataType: 'json',
            headers: {
                "X-CSRFTOKEN":getCookie("csrf_token")
            },
            success:function (resp) {
                if (resp.errno == '4101'){
                    location.href = '/login.html'
                }else if(resp.errno == '0'){
                    $('.house-image-cons').append(`<img src="${resp.data.image_url}">`);
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })
});